package com.ctbu.web;

import com.ctbu.entity.FinancialContracts;
import com.ctbu.service.FinancialContractsService;
import com.ctbu.tools.ResponseResult;
import com.ctbu.tools.StatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 金融衍生品合约信息表，包含合约ID、产品ID、用户ID、起始日期、结束日期、备注、合约单位、当前价格和约定价格等信息 前端控制器
 * </p>
 *
 * @author tanyan
 * @since 2024年06月27日
 */

@Slf4j
@RestController
@RequestMapping("/financialContracts")
public class FinancialContractsController {
    @Autowired
    private FinancialContractsService financialContractsService;

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseResult<Object> list(
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "5") int pageSize,
            @RequestParam(value = "productId",required = false,defaultValue = "") Integer productId,
            @RequestParam(value = "userId",required = false,defaultValue = "") Integer userId
    ){
        return financialContractsService.selectList(pageNum,pageSize,productId,userId);
    }
    /**
     * 根据id查询合约信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult<Object> queryFinancialContractsById(@PathVariable String id) {
        FinancialContracts result = financialContractsService.getById(id);
        return ResponseResult.success(result,"查询成功");
    }

    /**
     * 增加合约信息
     * @param financialContracts
     * @return
     */
    @PostMapping
    public ResponseResult<Object> insert(@RequestBody FinancialContracts financialContracts){
        boolean result = financialContractsService.save(financialContracts);

        if (result){
            return ResponseResult.success(result,"下单成功");

        }
        return ResponseResult.fail(StatusEnum.INTERNAL_SERVER_ERROR,"下单失败");
    }

    /**
     * 修改合约信息
     * @param financialContracts
     * @return
     */

    @PutMapping
    public ResponseResult<Object> update(@RequestBody FinancialContracts financialContracts){

        boolean result = financialContractsService.updateById(financialContracts);
        if (result){
            return ResponseResult.success(result,"修改合约信息成功");

        }
        return ResponseResult.fail(StatusEnum.INTERNAL_SERVER_ERROR,"修改合约信息失败");
    }


    @DeleteMapping("/{id}")
    public ResponseResult<Object> delete(@PathVariable String id){
        boolean result = financialContractsService.removeById(id);
        if (result){
            return ResponseResult.success(result,"撤单成功");
        }
        return ResponseResult.fail(StatusEnum.INTERNAL_SERVER_ERROR,"撤单失败");

    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping("/queryAll")
    public ResponseResult<Object> queryAll(){
        List<FinancialContracts> list = financialContractsService.list();
        return ResponseResult.success(list,"查询成功");
    }

//    @GetMapping("/exportExcel")
//    public void exportExcel(
//            @RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum,
//            @RequestParam(value = "pageSize",required = false,defaultValue = "5") int pageSize,
//            @RequestParam(value = "productId",required = false,defaultValue = "") Integer productId,
//            HttpServletResponse response
//    ) throws IOException {
//        ResponseResult<Object> result = financialContractsService.selectList(pageNum, pageSize,productId);
//        PageInfo<FinancialContracts> pageInfo = (PageInfo<FinancialContracts>) result.getData();
//        //获取查询的数据
//        List<FinancialContracts> list = pageInfo.getList();
//        //导出工作
//        // 设置响应头
//        response.setContentType("application/vnd.ms-excel");
//        response.setCharacterEncoding("utf-8");
//        // 设置防止中文名乱码
//        String filename = URLEncoder.encode("贷款产品信息", "utf-8");
//        // 文件下载方式(附件下载还是在当前浏览器打开)
//        response.setHeader("Content-disposition", "attachment;filename=" + filename + ".xlsx");
//        //导出
//        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("贷款产品信息", "第一页"), FinancialContracts.class, list);
//        //导出流信息到浏览器
//        workbook.write(response.getOutputStream());
//        //释放资源
//        workbook.close();
//        log.info("导出数据成功");
//    }

}
